<?php
include "include/config.inc.php";
if(!isset($_SESSION['s_activId']))
{
  header('location:index.php');
}

$filterOn = isset($_GET['filterOn']) ? $_GET['filterOn'] : '';
//select Account : Start
$selectAccount = "SELECT accountId,accountName,isParty,partyCode
                    FROM account
                   WHERE 1 = 1 ";

if($filterOn == 'dayBook')
{
  $selectAccount .= " AND isDayBook = 1";
}

if($filterOn == 'party')
{
  $selectAccount .= " AND isParty = 1";
}

if($filterOn == 'general')
{
  $selectAccount .= " AND isParty = 0
                      AND isDayBook = 0";
}

$selectAccount .= " ORDER BY accountName";
$selectAccountRes = mysql_query($selectAccount);
$accArray = array();
$a = 0;
$totCrAmt = 0;
$totDrAmt = 0;
$totAmt   = 0;
while($accRow = mysql_fetch_array($selectAccountRes))
{
	$accArray[$a]['partyCode'] = '';
	if($accRow['isParty'] == 1)
	{
    $accArray[$a]['partyCode'] = $accRow['partyCode'];
	}	
  $accArray[$a]['accountId']   = $accRow['accountId'];
  $accArray[$a]['accountName'] = $accRow['accountName'];
  
  $accArray[$a]['drAmount'] = 0;
  $accArray[$a]['crAmount'] = 0;
  
	$selOpeningDetail = "SELECT openingDrCr,opening
	                       FROM account
	                      WHERE accountId = ".$accRow['accountId'];
	$selOpeningDetailRes = mysql_query($selOpeningDetail);
	while($detRow = mysql_fetch_assoc($selOpeningDetailRes))
	{
    $accArray[$a]['openingDrCr'] = $detRow['openingDrCr'];
    $accArray[$a]['opening']     = $detRow['opening'];
	}
	
	$selTransDetail = "SELECT crAmount
	                     FROM transdetail
	                    WHERE crAccountId = ".$accRow['accountId'];
	$selTransDetailRes = mysql_query($selTransDetail);
	while($detRow = mysql_fetch_assoc($selTransDetailRes))
	{
    $accArray[$a]['crAmount'] += $detRow['crAmount'];
	}
	
	$selTransDetail = "SELECT drAmount
	                     FROM transdetail
	                    WHERE drAccountId = ".$accRow['accountId'];
	$selTransDetailRes = mysql_query($selTransDetail);
	while($detRow = mysql_fetch_assoc($selTransDetailRes))
	{
    $accArray[$a]['drAmount'] += $detRow['drAmount'];
	}	
	
	if($accArray[$a]['openingDrCr'] == 'Cr')
	{
	  $accArray[$a]['finalAmount'] = ($accArray[$a]['crAmount'] - $accArray[$a]['drAmount']) + $accArray[$a]['opening'];
	}
	else
	{
	  $accArray[$a]['finalAmount'] = ($accArray[$a]['crAmount'] - $accArray[$a]['drAmount']) - $accArray[$a]['opening'];
	}
	

  if($accArray[$a]['finalAmount'] > 0)
  {
		$totCrAmt += $accArray[$a]['finalAmount'];
	}
	else
	{
		$totDrAmt += $accArray[$a]['finalAmount'];
	}
  $a++;
}
$totAmt = $totDrAmt + $totCrAmt;
//select Account : End
include("bottom.php");
$smarty->assign('totCrAmt',$totCrAmt);
$smarty->assign('totDrAmt',$totDrAmt);
$smarty->assign('totAmt',$totAmt);
$smarty->assign('accArray',$accArray);
$smarty->assign('filterOn',$filterOn);
$smarty->display('summary.tpl');  
?>